#ifndef CDCELECENERMETERSINGLE_H
#define CDCELECENERMETERSINGLE_H

#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDateTime>
#include <QList>
#include <QDebug>
#include <QString>
#include <QChar>
#include <math.h>

class CDCElecEnerMeterSingle : public QSerialPort
{
    Q_OBJECT
private:
    explicit CDCElecEnerMeterSingle(QObject *parent = 0);
    explicit CDCElecEnerMeterSingle(const QSerialPortInfo &serialPortInfo, QObject *parent = nullptr);
    explicit CDCElecEnerMeterSingle(const QString &strPortName, QSerialPort::BaudRate baud,QObject *parent = 0);
    static  CDCElecEnerMeterSingle *m_instanse; //饿汉单例模式
    //用于垃圾自动回收
    class GarbageCollector {
    public:
        ~GarbageCollector() {
            if (CDCElecEnerMeterSingle::m_instanse) {
                delete CDCElecEnerMeterSingle::m_instanse;
                CDCElecEnerMeterSingle::m_instanse = nullptr;
                qDebug()<<"huishou";
            }
        }
    };
    static GarbageCollector gc; //声明垃圾回收对象

public:
    //获得单利对象
    static CDCElecEnerMeterSingle *GetSingelton()
    {
        return m_instanse;
    }
    //对外接口
    //设置参数
    void setSerialPara(const QString &strPortName, QSerialPort::BaudRate baud);
    float getCurrentEner() const;
    float getInitialEner() const;
    void setInitialEner(float initialEner);

signals:
     void signalGetReady(); //接受一次有效数据就发送一次信号
private:
    //字段
    float _currentEner; //当前电能
    float _initialEner; //初始电能

    void paraInit(); //初始化参数函数
    void dealRec(); //接受处理函数

public slots:
};

#endif // CDCELECENERMETER_H


#if 0 //测试直流电能表
    CDCElecEnerMeter *gps=new CDCElecEnerMeter(tr("COM21"),QSerialPort::Baud2400);
    gps->open(QIODevice::ReadWrite);
    if(gps->isOpen()==true)
    {
        qDebug()<<tr("open true");
        gps->write("hello");
    }


#endif
